package com.xyh.transaction.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.xyh.transaction.common.Constant;
import com.xyh.transaction.entity.User;
import com.xyh.transaction.exception.DuplicateUsernameException;
import com.xyh.transaction.exception.ReadExcelException;
import com.xyh.transaction.mapper.UserMapper;
import com.xyh.transaction.utils.MappingUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;

@Slf4j
public class ImportUserVoListener implements ReadListener<ImportExcelUserVo> {

    private final Boolean isCover;

    private final UserMapper userMapper;

    private final PasswordEncoder passwordEncoder;

    public ImportUserVoListener(Boolean isCover, UserMapper userMapper, PasswordEncoder passwordEncoder) {
        this.isCover = isCover;
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }


    @Override
    public void invoke(ImportExcelUserVo importExcelUserVo, AnalysisContext context) {
        Integer rowIndex = context.readRowHolder().getRowIndex() + 1; // 跳过表头
        log.info("开始读取第：{} 行数据", rowIndex);
        User user = MappingUtil.mapObject(importExcelUserVo, User.class);
        String username = user.getUsername();
        if (StringUtils.isBlank(username)) {
            throw new ReadExcelException("用户名为空");
        }
        User byName = userMapper.findByName(username);
        verifyUserField(user);
        if (isCover) {
            if (byName == null) {
                throw new ReadExcelException(username+" 用户不存在");
            }
            userMapper.updateUserByUserName(user);
        } else {

            if (byName != null) {
                throw new DuplicateUsernameException(username);
            }
            user.setPassword(passwordEncoder.encode(Constant.DEFAULT_PASSWORD));

            userMapper.insert(user);
        }
    }

    private void verifyUserField(User user) {
        if (user.getGender() == null) {
            throw new ReadExcelException("性别字段为空");
        }

        if (user.getStatus() == null) {
            throw new ReadExcelException("状态字段为空");
        }

        if (user.getCreateTime() == null) {
            throw new ReadExcelException("创建时间字段为空");
        }

        if (user.getEmail() == null) {
            throw new ReadExcelException("邮箱字段为空");
        }

        if (user.getIdCard() == null) {
            throw new ReadExcelException("身份证字段为空");
        }

        if (user.getAge() == null) {
            throw new ReadExcelException("年龄字段为空");
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        int rowIndex = context.readRowHolder().getRowIndex() + 1;
        log.error("第：{} 行数据读取报错,原因：" , exception.getMessage());
        throw new ReadExcelException("第" + rowIndex + "行数据读取报错：" + exception.getMessage(), exception);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("数据读取结束！");
    }
}
